<?php

namespace app\admin\controller\finance;

use app\common\controller\Backend;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;
use app\common\model\Config;
use app\admin\model\Middleman;
use app\admin\model\ShopFollow;
use app\admin\model\ShopPlatform;
use app\admin\model\Admin;
use app\admin\model\Play;
use app\common\model\OrderProof;

/**
 * 店铺管理
 *
 */
class MiddlemanPerformances extends Backend
{
    /**
     * Play模型对象
     * @var \app\admin\model\Play
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        // $this->model = new \app\admin\model\Play;
        $this->model = new \app\admin\model\Shop;
        // 当前用户身份，如果是业务员，业务员筛选那里只显示自己的名字
        $auth_group_access = Db::name('auth_group_access')->where('uid',$this->auth->id)->value('group_concat(group_id)');
        $explode = explode(',', $auth_group_access);
        $service_admin_id = [];
        if (in_array(5,$explode) && !in_array(1,$explode)) {
            $salesmanList = [];
            $member = Db::name('admin')->where('leader',$this->auth->id)->field('nickname,id')->select();
            foreach ($member as $key => $value) {
                $salesmanList[$value['id']] = $value['nickname'];
            }
        }elseif (in_array(1,$explode)) {
            $salesmanList = [];
            $salesman = $this->salesmanList();
            foreach ($salesman as $key => $value) {
                $salesmanList[$value['id']] = $value['nickname'];
            }
        } elseif (in_array(3,$explode) && !in_array(1,$explode)) {// 业务员
            $salesmanList = [];
            $salesmanList[$this->auth->id] = $this->auth->nickname;
        }
        $this->assignconfig(['salesmanList' => $salesmanList]);
        $this->db2 = Db::connect(config('database.db2'));
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            // 有已完成的活动订单
            $filter = $_GET['filter'];
            $filterarr = [];
            if ($filter) {
                $filterarr = json_decode($filter,1);
            }
            $where2 = 'm.no>0';
            // 当前用户身份，如果是业务员，则只显示自己的活动
            $auth_group_access = Db::name('auth_group_access')->where('uid',$this->auth->id)->value('group_concat(group_id)');
            $explode = explode(',', $auth_group_access);
            $service_admin_id = [];
            if (in_array(5,$explode) && !in_array(1,$explode)) {
                $member = Db::name('admin')->where('leader',$this->auth->id)->value('group_concat(id)');
                $access_user = explode(',', $member);
                $access_user[] = $this->auth->id;
                $service_admin_id = $access_user;
                $salesman_str = implode(',', $service_admin_id);
                $where2 .= ' and m.salesman_admin_id in ('.$salesman_str.')';
            } elseif (in_array(3,$explode) && !in_array(1,$explode)) {// 业务员
                $service_admin_id = [$this->auth->id];
                $salesman_str = implode(',', $service_admin_id);
                $where2 .= ' and m.salesman_admin_id in ('.$salesman_str.')';
            }
            if (isset($filterarr['nickname'])) {
                $where2 .= ' and m.salesman_admin_id='.$filterarr['nickname'];
            }
            if (isset($filterarr['create_dt'])) {
                $date = explode(' - ', $filterarr['create_dt']);
                $where2 .= ' and uml.create_dt >"'.$date[0].'" and uml.create_dt<"'.$date[1].'"';
            }
            if (isset($filterarr['city'])) {
                $where2 .= ' and s.city='.$filterarr['city'];
            }
            if (isset($filterarr['no'])) {
                $where2 .= ' and m.no ="'.$filterarr['no'].'"';
            }
            if (isset($filterarr['place_time'])) {
                $date = explode(' - ', $filterarr['place_time']);
                $where2 .= ' and po.place_time >="'.strtotime($date[0]).'" and po.place_time<"'.strtotime($date[1]).'"';
            }
            $data = $this->db2->name('middleman')
                ->alias('m')
                ->field('m.*,po.shop_name,po.play_no,po.service_charge_amt as service_amt,s.city,uml.money as rebate_amt,uml.create_dt,po.order_status,po.place_time,po.wm_order_sn')
                ->join('chbc_shop s','m.id=s.middleman_id')
                // ->join('chbc_play p','s.shop_no=p.shop_no and super_play_no=0')
                ->join('chbc_play_order po','po.shop_no=s.shop_no')
                ->join('user_money_log uml','uml.tran_no=po.play_order_no and uml.log_type=12')
                ->where($where2)
                ->order('uml.create_dt DESC,m.id DESC')
                ->paginate($limit);
            $list = [];
            foreach ($data as $key => $row) {
                $list[$key] = $row;
            }
            $admin = $this->db2->name('auth_group_access')->alias('aga')->join('chbc_admin a','aga.uid=a.id')->where(['aga.group_id'=>3])->select();
            $nickname = [];
            foreach ($admin as $v) {
                $nickname[$v['id']] = $v['nickname'];
            }
            foreach ($list as &$value) {
                $value['amount'] = 1;
                $value['yc'] = $value['order_status']==-4?1:0;
                $value['money'] = $value['rebate_amt'];
                $value['nickname'] = $nickname[$value['salesman_admin_id']];
            }
            $result = array("total" => $data->total(), "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

}
